package cn.bearspark.article.provider.dao.buffer;

import cn.bearspark.article.provider.dao.co.ArticleCO;
import cn.bearspark.article.provider.dao.key.ArticleBufferKeyBuilder;
import cn.bearspark.article.provider.dao.mapper.ArticleBaseMapper;
import cn.bearspark.framework.redis.starter.buffer.hash.un.RedisHashBuffer;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 专栏基础数据的 hash 数据结构的缓存器
 *
 * @author f
 */
@Component
public class ArticleBaseHashBuffer extends RedisHashBuffer<ArticleCO> {

    /**
     * 设置专栏数据缓存的有效期（单位：s），默认是 5min
     */
    private static final int ARTICLE_INFO_CACHE_EXPIRE = 60 * 5;

    /**
     * 专栏简单数据的字段名称集合
     */
    private final List<String> simpleInfoList;

    public ArticleBaseHashBuffer(RedisTemplate<String, Object> redisTemplate,
                                 ArticleBufferKeyBuilder articleBufferKeyBuilder,
                                 ArticleBaseMapper articleBaseMapper) {
        super(
                redisTemplate,
                articleBufferKeyBuilder,
                ARTICLE_INFO_CACHE_EXPIRE,
                ArticleCO.class,
                articleBaseMapper
        );

        this.simpleInfoList = List.of("articleId", "happeningId", "coverUrl", "declaration", "likeNum",
                "viewNum", "commentNum", "collectNum", "pubTime");
    }

    /**
     * 映射专栏的简单数据
     *
     * @param articleIdList 待获取简单数据的专栏 id 集合
     * @return 专栏的简单数据的映射，key：专栏 id，value：专栏简单数据
     */
    public Map<Long, ArticleCO> mapSimple(List<Long> articleIdList) {
        return super.map(articleIdList); // TODO 使用 mapLess()
    }
}
